package com.zhiyou.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.zhiyou.pojo.JsonResult;
import com.zhiyou.pojo.User;

public class LoginInterceptor extends HandlerInterceptorAdapter {

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		
		if (user == null) {
			JsonResult<String> jsonResult = new JsonResult<String>(101, null, null);
			ObjectMapper mapper = new ObjectMapper();
			String json = mapper.writeValueAsString(jsonResult);
			response.getWriter().write(json);
			return false;
		} else if (user.getStatus() == 0) {
			JsonResult<String> jsonResult = new JsonResult<String>(102, null, null);
			ObjectMapper mapper = new ObjectMapper();
			String json = mapper.writeValueAsString(jsonResult);
			response.getWriter().write(json);
			return false;
		} else if (user.getStatus()==1) {
			JsonResult<String> jsonResult = new JsonResult<String>(103, null, null);
			ObjectMapper mapper = new ObjectMapper();
			String json = mapper.writeValueAsString(jsonResult);
			response.getWriter().write(json);
			return false;
		} else if (user.getStatus()==2) {
			JsonResult<String> jsonResult = new JsonResult<String>(104, null, null);
			ObjectMapper mapper = new ObjectMapper();
			String json = mapper.writeValueAsString(jsonResult);
			response.getWriter().write(json);
			return false;
		}else if (user.getStatus() == 3) {
			JsonResult<String> jsonResult = new JsonResult<String>(105, null, null);
			ObjectMapper mapper = new ObjectMapper();
			String json = mapper.writeValueAsString(jsonResult);
			response.getWriter().write(json);
			return false;
		}
		return true;

	}

}
